package com.hejjon.knowledgebase.model.wrapper;

import com.alibaba.fastjson.annotation.JSONField;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.io.Serializable;
import java.util.List;

/**
 * @author:
 * @date: 2022-12-09 15:22:11
 * @since: 1.0
 */
public class DataGrid<T> implements Serializable {


    private static final long serialVersionUID = 929861446679595891L;

    /**
     * 页码
     */
    @JSONField(ordinal = 0)
    private int page;

    /**
     * 每页记录数
     */
    @JSONField(ordinal = 1)
    private int limit;

    /**
     * 当前页的数量 应该 <= limit
     */
    @JSONField(ordinal = 2)
    private int size;

    /**
     * 总页数
     */
    @JSONField(ordinal = 3)
    private int pages;

    /**
     * 总记录数
     */
    @JSONField(ordinal = 4)
    private int total;

    /**
     * 结果列表
     */
    @JSONField(ordinal = 5)
    private List<T> rows;

    public DataGrid() {
    }

    public DataGrid(List<T> rows, int total) {
        this.rows = rows;
        this.total = total;
    }

    public DataGrid(Page<T> page) {
        this.rows = page.getRecords();
        this.page = Long.valueOf(page.getCurrent()).intValue();
        this.limit = Long.valueOf(page.getSize()).intValue();
        this.size = this.rows.size();
        this.pages = Long.valueOf(page.getPages()).intValue();
        this.total = Long.valueOf(page.getTotal()).intValue();
    }


    public static <T> DataGrid<T> of() {
        return new DataGrid<>();
    }

    public static <T> DataGrid<T> of(List<T> rows, int total) {
        return new DataGrid<>(rows, total);
    }

    public static <T> DataGrid<T> of(Page<T> page) {
        return new DataGrid<>(page);
    }


    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public int getLimit() {
        return limit;
    }

    public void setLimit(int limit) {
        this.limit = limit;
    }

    public int getSize() {
        return size;
    }

    public void setSize(int size) {
        this.size = size;
    }

    public int getPages() {
        return pages;
    }

    public void setPages(int pages) {
        this.pages = pages;
    }

    public int getTotal() {
        return total;
    }

    public void setTotal(int total) {
        this.total = total;
    }

    public List<T> getRows() {
        return rows;
    }

    public void setRows(List<T> rows) {
        this.rows = rows;
    }
}



